//document.cookie="token=a6740e72835384c82d67fdb8407eaca99d471241"
getCookie=function(name){
	var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr!=null)return unescape(arr[2]); return "";
}
setCookie=function(name,value,days){
	var date=new Date().getTime();
	days?days:30;
	date+=days*24*60*60*1000;
	document.cookie=name+"="+escape(value)+";path=/;expires="+new Date(date).toUTCString();
}
var token= getCookie("token");
var headObj={
	inits:function(){
		this.searchFun();
		this.ajax();
		this.headPicFun();
	},
	Interface:{
		isLogin:"http://ww.yc630.com/api/index.php/Buyer/Login/isLogin" , //是否登录
		isSeller:"http://ww.yc630.com/api/index.php/Seller/Seller/isSeller"//判断买卖家 0 是卖家  1非卖家
	},
	ajax:function(){
		//console.log(headObj.Interface.isLogin)
		//common.ajaxFun(headObj.Interface.isLogin,"POST",{token:token},headObj.isLoginFun); //是否登录
		$.ajax({
			url:headObj.Interface.isLogin,
			type:'POST',
			data:{
				"token":token
			},
			dataType:'json',
			success:function(res) {
				if (res.code == 0) {
					$(".loginOrRe").hide();
					$("#headPic").attr('src',res.data.avatar);
					$("#headPic").show();
				} else {
					$("#headPic").hide();
					$(".loginOrRe").show();
				}
			}
		})
	},
	searchFun:function(){
		$("#searchBtn").click(function(){
			var searchtype=$(this).siblings("select").val(),
				searchkey=$(this).siblings("input").val();
			window.location.href="./CommoditySearch.html?searchtype="+searchtype+"&searchkey="+escape(searchkey);
		})
	},
	headPicFun:function(){

		$("#headPic").on("click",function(){

			$.ajax({
				url:headObj.Interface.isSeller,
				type:'POST',
				data:{
					"token":token
				},
				dataType:'json',
				success:function(res) {
					if (res.code==0) {
						if (getCookie("flag")!="") {
							if (getCookie("flag")==0) {//到买家
								window.location.href="./../page/ddlb.html";
								setCookie("flag",1);
							}else if (getCookie("flag")==1) {//到卖家
								window.location.href="./../page/goods_house.html";
								setCookie("flag",0);
							};
						}else if(getCookie("flag")=="") {
							setCookie("flag",0);
							window.location.href="./../page/goods_house.html";
						};
					}else if (res.code==1){
						window.location.href="./../page/ddlb.html";
					};
				}
			})

		});

	}
	//isLoginFun:function(res){
	//	if (res.code==0) {
	//		$("#headPic").show();
	//		$(".loginOrRe").hide();
	//	}else {
	//		$("#headPic").hide();
	//		$(".loginOrRe").show();
	//	};
	//	console.log(res)
	//}

}
headObj.inits()